# При общей парковке сначала паркуется Z. Потом Y, потом X.
# При парковке X всегда сначала паркуется Y чтоб быть уверенным, что X находится в зоне парковки.
# Так как на X стоят (пока) безконтактные датчики парковки, добавлен скрипт SENSORLESS_HOME_X.

[gcode_macro SENSORLESS_HOME_X]
gcode:
    {% set HOME_CUR = 1.00 %}                               # preset homing current
    {% set driver_config_x = printer.configfile.settings['tmc2130 stepper_x'] %}        # select stepper X
    {% set driver_config_x1 = printer.configfile.settings['tmc2130 dual_carriage'] %}   # select stepper DUAL_CARRIAGE
    {% set RUN_CUR_X = driver_config_x.run_current %}       # save during print current for X
    {% set RUN_CUR_DUAL_CARRIAGE = driver_config_x1.run_current %}  # save during print current for DUAL_CARRIAGE
    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR}    # Set current for sensorless homing X
    SET_TMC_CURRENT STEPPER=dual_carriage CURRENT={HOME_CUR}        # Set current for sensorless homing X
    G4 P1000                                                # Pause to ensure driver stall flag is clear
    G28 X                                                   # Home
    G90                                                     # Use absolute coordinates
    SET_DUAL_CARRIAGE CARRIAGE=0
    G0 X-10 F1200                                           # Move away X
    SET_DUAL_CARRIAGE CARRIAGE=1
    G0 X410 F1200                                           # Move away DUAL_CARRIAGE
    SET_DUAL_CARRIAGE CARRIAGE=0
    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={RUN_CUR_X}   # restore current during print
    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={RUN_CUR_DUAL_CARRIAGE} # restore current during print

[homing_override]
axes: xyz
gcode:
# If G28 with out params
    {% if params["X"] is undefined and params["Y"] is undefined and params["Z"] is undefined %}
        G28 Z
        G28 Y
        SENSORLESS_HOME_X
    {% else %}
# If G28 with Z param
        {% if 'Z' in params %}
            G28 Z
        {% endif %}
# If G28 with X param
        {% if 'X' in params %}
            G28 Y                                           # Be sure if X in park position (for endstops on main frame)
            SENSORLESS_HOME_X
        {% else %}
# If G28 with Y param
            {% if 'Y' in params %}
                G28 Y
            {% endif %}
        {% endif %}
    {% endif %}
